/*****************************************************************************\

File:   otkWTServerExample.java

Date          Build     Author        Version   Comments
22-Aug-12    P-20-13    asonar       $$1       Created
20-mar-15 P-30-05 gshmelev $$2  called GetCurrentSession

\****************************************************************************/

package com.ptc.otkjavaexamples;

import com.ptc.cipjava.*;
import com.ptc.pfc.pfcGlobal.*;
import com.ptc.pfc.pfcSession.*;
import com.ptc.pfc.pfcCommand.*;

import com.ptc.pfc.pfcSolid.*;
import com.ptc.pfc.pfcModel.*;
import com.ptc.pfc.pfcServer.*;

import com.ptc.wfc.wfcSession.*;
import com.ptc.wfc.wfcSolid.*;
import com.ptc.wfc.wfcFamily.*;

/*====================================================================*\
  FUNCTION : otkWTServerExample
  PURPOSE  : This example will demonstrate basic Windchill Server operations.
  It will first authenticate the browser followed by server register.
  It will retrieve an assembly (asm0001.asm) from disk, save it to workspace followed by check-in
  It will then download the just checked in assembly.
  It will also checkout the just checked in assembly.
\*====================================================================*/

class otkWTServerExample extends DefaultUICommandActionListener
{
    
    public otkWTServerExample() 
    {
    } 
    
    public void OnCommand () 
    {
	addWTServerExample();
    }
    
    /* Function to execute example code in current session */
    
    public static void addWTServerExample()
    {
	
	try
	    {
		System.out.println("- Server Operations Example -");
		System.out.println("==============================");
		
		Session session = pfcSession.GetCurrentSession ();
		
		/*Register the Windchill Server*/
		Server server = otkServerRegister(session);
		
		/*Retrieve, Save & Checkin set of models*/
		Models models = otkServerCheckinOperation(session, server);
		
		/*Download the Checked-in models*/
		otkServerCheckoutORDownloadOperation(server, models, false);
		
		/*Checkout the Checked-in models*/
		otkServerCheckoutORDownloadOperation(server, models, true);
		
		server.Unregister();
		
	    }
	catch (Throwable e)
	    {
		System.out.println ("Exception caught: "+e);
		e.printStackTrace ();
	    }
    }
    
    
    //*******************************************************	
	public static Server otkServerRegister (Session session)
    {
	try
	    {
		
	String WC_USERNAME = "wtadmin";   /* Change this as appropriate */
	String WC_PASSWORD = "wtadmin";   /* Change this as appropriate */
	String SERVER_ALIAS = "Java_Server";   /* Change this as appropriate */
	String SERVER_URL = "http://hfa-oracle.ptcnet.ptc.com:10300/Windchill";  /* Change this as appropriate */
	String SERVER_WORKSPACE = "Workspace on Site";  /* Change this as appropriate */
	
	System.out.println("Username: " + WC_USERNAME);
	System.out.println("Password: " + WC_PASSWORD);
	System.out.println("Alias: " + SERVER_ALIAS);
	System.out.println("Url: " + SERVER_URL);
	System.out.println("Workspace: " + SERVER_WORKSPACE);
	
	session.AuthenticateBrowser(WC_USERNAME, WC_PASSWORD);
	
	System.out.println("Browser Authentication Success");
	
	Server server = session.RegisterServer(SERVER_ALIAS, SERVER_URL, SERVER_WORKSPACE);
	
	System.out.println ("Server Register Success");
	
	return (server);
	
	    }

       catch (Exception e)
            {
	System.out.println("1: " + e);
	return null;
            }
   
    }

	//*****************************************************************************
	public static Models otkServerCheckinOperation (Session session, Server server)
    {
	try
	    {
		ModelDescriptor AsmDesc = pfcModel.ModelDescriptor_Create(ModelType.MDL_ASSEMBLY, "asm0001", "");
		
		Model model = session.RetrieveModel(AsmDesc);
		
		System.out.println("Model Retrieve Success");
		
		server.Activate();
		
		System.out.println("Server Activate Success");
		
		model.Save();
		
		System.out.println("Model Save Success");
		
		server.UploadObjects(model);
		
		System.out.println("Model Upload Success");
		
		CheckinOptions CheckinOpts = pfcServer.CheckinOptions_Create();
		
		server.CheckinObjects(model, CheckinOpts);	
		
		System.out.println("Model Checkin Success");
		
		Models models =  Models.create();
		
		models.append(model);
		
		return (models);
		
	    }
	
	catch (Exception e)
	    {
		System.out.println("1: " + e);
		return null;
	    }
	
    }
    
    
    //********************************************************
	    public static void otkServerCheckoutORDownloadOperation (Server server, 
								     Models models, boolean DoCheckout)
    {
	try
	    {
		CheckoutOptions CO_Opts = pfcServer.CheckoutOptions_Create();
		
		CO_Opts.SetDependency(ServerDependency.SERVER_DEPENDENCY_ALL);
		
		for (int i=0 ; i<models.getarraysize() ; i++)
		    {
			server.CheckoutObjects(models.get(i), null, DoCheckout, CO_Opts);
			
			if (DoCheckout)
			    System.out.println("Model Checkout Success");					
			else
			    System.out.println("Model Download Success");
			
		    }
	    }
	
	catch (Exception e)
	    {
		System.out.println("1: " + e);
	    }
	
	
    }
    
}
